'use strict';

const Controller = require('egg').Controller;

class ListController extends Controller {
    async add() {
        let {ctx,service} = this;
        let {role} = ctx.info;
        if(role === 'v0'){  //v0
            //超级管理员
            let {nick_name,phone,tags,role,id_card,address,info,end} = ctx.request.body;
            if(nick_name && phone && tags && id_card && address && info && end){
                try{
                    await service.list.add(nick_name,phone,tags,role,id_card,address,info,end);
                    ctx.body = {
                        code:1,
                        msg:'添加成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }else{
                ctx.body = {
                    code:2,
                    msg:'参数有误'
                }
            }
        }else{
            ctx.body = {
                code:5,
                msg:'没有添加的权限'
            }
        }
        
    }
    async del(){
        let {ctx,service} = this;
        let {role} = ctx.info;
        if(role === 'v0'){
            //进行删除
            let {ids} = ctx.query;  //1,2,3
            try{
                let arr = ids.split(','); //[1,2,3]
                console.log(ids);
                for(let i=0;i<arr.length;i++){
                    await service.list.del(arr[i]);
                }
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:5,
                msg:'没有权限删除'
            }
        }
        
    }
    async list(){
        let {ctx,service} = this;
        let {pagenum=1,limit=5} = ctx.query;
        let list = await service.list.list(pagenum,limit);
        let total = await service.list.total();
        console.log("total",total)
        ctx.body = {
            code:1,
            data:list,
            total:total[0]['count(*)']
        }
    }
    async edit(){
        let {ctx,service} = this;
        let {role} = ctx.info;
        if(role === 'v0' || role === 'v1'){  //v0
            //超级管理员
            let {nick_name,phone,tags,role,id_card,address,info,end,id} = ctx.request.body;
            if(nick_name && phone && tags && id_card && address && info && end && id){
                try{
                    await service.list.edit(nick_name,phone,tags,role,id_card,address,info,end,id);
                    ctx.body = {
                        code:1,
                        msg:'修改成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }else{
                ctx.body = {
                    code:2,
                    msg:'参数有误'
                }
            }
        }else{
            ctx.body = {
                code:5,
                msg:'没有修改的权限'
            }
        }
    }
}

module.exports = ListController;
